package com.walkline.app;

import java.util.Vector;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
import net.rim.device.api.util.Persistable;

import com.walkline.util.Enumerations.ShortcutKey;

public final class RedFlowerAppConfig implements Persistable
{
	public static final String APP_NAME = "Red Flower for ";
	public static final String APP_TITLE = "Red Flower for you";
	public static final String UNDERLINE = "\u0332";

	public static final Background bgColor_lightGray = BackgroundFactory.createSolidBackground(Color.LIGHTGRAY);
	public static final Background bgColor_white = BackgroundFactory.createSolidBackground(Color.WHITE);
	public static final Background bgColor_black = BackgroundFactory.createSolidBackground(Color.BLACK);
	public static final Background bgColor_Gradient = BackgroundFactory.createLinearGradientBackground(Color.GRAY, Color.GRAY, Color.BLACK, Color.BLACK);
	public static final Border border_Transparent = BorderFactory.createRoundedBorder(new XYEdges(16,16,16,16), Color.BLACK, 200, Border.STYLE_FILLED);
	public static final Background bg_Transparent = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 200);

	public static final Font FONT_SETTINGS_TITLE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);
	public static final Font FONT_SETTINGS_DESCRIPTION = Font.getDefault().derive(Font.ITALIC, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);

	public static final Font FONT_ABOUT_TITLE = Font.getDefault().derive(Font.BOLD, Font.getDefault().getHeight(Ui.UNITS_pt)+2, Ui.UNITS_pt);
	public static final Font FONT_ABOUT_HEADLINE = Font.getDefault().derive(Font.BOLD | Font.ITALIC, Font.getDefault().getHeight(Ui.UNITS_pt), Ui.UNITS_pt);
	public static final Font FONT_ABOUT_SMALL = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)-1, Ui.UNITS_pt);
	public static final Font FONT_ABOUT_LARGE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)+1, Ui.UNITS_pt);

	public static final Font FONT_MAIN_TITLE = Font.getDefault().derive(Font.PLAIN, Font.getDefault().getHeight(Ui.UNITS_pt)+2, Ui.UNITS_pt);

	public static final String DONATE_$1_PurchasingAppName = "Red Flower for you";
	public static final String Donate_$1_Name = "Donate 1 dollar";
	public static final String Donate_$1_SKU = "redflower_for_you_donate_1_dollar";

	public static final String[] VendorID = {
		"1", "100", "101", "102", "103", "104", "105", "106", "107", "109", "111", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", 
		"149", "150", "151", "152", "153", "154", "155", "156", "158", "159", "160", "161", "162", "164", "165", "166", "167", "168", "169", "170", "171", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "186", "188", "189", "190", "191", "194", "195", "197", "198", "199", "202", "204", "205", "210", "212", 
		"213", "214", "215", "216", "217", "218", "220", "222", "224", "225", "227", "228", "229", "230", "236", "237", "238", "239", "240", "241", "245", "247", "248", "250", "251", "254", "255", "258", "259", "269", "271", "273", "274", "277", "278", "280", "282", "285", "286", "288", "294", "295", "296", "297", "298", "299", "300", 
		"301", "302", "303", "308", "309", "310", "312", "313", "315", "316", "317", "318", "319", "322", "323", "324", "326", "327", "329", "331", "332", "336", "337", "339", "340", "352", "353", "369", "370", "371", "373", "375", "376", "377", "378", "379", "380", "381", "382", "383", "384", "385", "386", "387", "388", "389", "390", 
		"391", "392", "394", "395", "396", "397", "398", "399", "415", "416", "417", "418", "421", "431", "432", "433", "434", "435", "436", "437", "439", "442", "451", "462", "477", "480", "488", "492", "501", "510", "511", "512", "513", "600", "601", "602", "603", "604", "605", "606", "608", "609", "610", "611", "612", "613", "614", 
		"617", "618", "619", "620", "622", "624", "626", "628", "209", "0", "163", "-1"};
	public static final String[] VendorName = {
		"RIM Engineering Prototypes (DVT测试机)", "T-Mobile US", "Cingular Wireless", "ATT", "Nextel/Nextel International - Brazil/Nextel International - Peru/Nextel International - Mexico", "Sprint", "Verizon Wireless", "AllTel", "Rogers", "Bell Mobility", "O2 DE", "Telfort", "T-Mobile Austria - AT/T-Mobile Germany - DE", "TIM Italy",
		"Hutchison", "Bouygues", "Vodafone SFR (France)", "Orange France/Orange Senegal/Orange Ivory Coast/Orange Caraibe/Orange Madagascar", "Vodafone UK/Vodafone Netherlands/Vodafone Ghana/Vodafone Ireland", "AMX Telcel Mexico", "Telstra", "T-Mobile UK/T-Mobile Croatia - HR", "Vodafone Germany", "O2 UK/O2 Ireland/O2 IoM (Manx Telecom)/Tesco Mobile",
		"Telus", "SMART Philippines", "StarHub", "Telefonica Spain", "Vodafone Switzerland (SwissCom)", "Cable & Wireless West Indies/Cable & Wireless Channel Islands/Cable & Wireless Panama/Cable & Wireless TSTT", "Vodafone Italy", "Vodafone Spain", "T-Mobile Netherlands - NL", "Cincinnati Bell Wireless", "Telefonica Group/ Group", 
		"Vodafone Austria (Mobilkom)/VIP Mobile Serbia", "Vodafone Australia", "Vodafone Ireland/Vodafone Airtel (Jersey)/Vodafone Lithuania (Bite)/Vodafone Latvia (Bite)", "Telenor Sweden", "CSL", "Orange UK/Orange Kenya/Orange Botswana/Orange Mauritius/Everything Everywhere/T-Mobile UK", "Vodafone New Zealand", "SingTel", "Globe", 
		"Optus", "Mobistar Belgium", "Vodafone Hungary", "Bharti (Airtel)/Airtel Sri Lanka/Bharti Seychelles/Airtel Bangladesh", "KPN", "Wind Hellas", "Vodafone Belgium (Proximus)", "Vodafone Portugal", "TIM Brazil", "BT Mobile", "Verizon Wireless Wholesale - EarthLink", "E-Plus", "BASE", "Dobson Communications", "Vodafone Egypt", 
		"Orange Switzerland/Orange Austria", "T-Mobile", "Maxis", "TDC Denmark", "Vodafone M1/Vodafone Dialog/Vodafone Celcom/XL Indonesia", "Vodacom South Africa", "T-Mobile Poland (PTC)", "T-Mobile Czech - CZ/T-Mobile Slovakia - SK", 
		"T-Mobile Hungary - HU", "MTN - South Africa/MTN - Nigeria/MTN - Cyprus/MTN - Ghana/MTN - Cameroon/MTN - Uganda/MTN Rwanda/MTN - Swaziland/MTN - Congo-Brazzaville/MTN - Benin/MTN Botswana/MTN - Liberia", "Entel PCS (TIM Chile)", "Orange Spain/Orange Dominicana", "SmarTone HK", "Verizon Wireless Wholesale - TCS/CloseCall", 
		"AVEA", "FAST 100", "Turkcell", "Partner", "Vodafone Netherlands", "Orange Poland/Orange Romania/Orange NL/Orange Slovakia/Orange Spain Global", "Telkomsel", "Vodafone Greece", "USCC", "Mobilink", "Velocita Wireless", "Vodafone Luxembourg", "Vodafone Iceland", "Vodafone Romania (Mobifon)", "Vodafone Czech", "Zain Bahrain", 
		"T-Mobile Slovakia - SK", "China Mobile(中国移动)/Telling(天音科技)", "Movilnet/Movilnet GSM", "Personal Argentina", "Etisalat", "CBeyond", "AMX Group/ Group", "Telefonica Venezuela", "Telefonica Brazil", "Orange Romania/Orange Moldova/Orange Poland/Orange Slovakia", "KTPowertel", "DoCoMo", "Vodafone Bulgaria (Mobiltel)", 
		"PCCW", "Hawaiian Telcom/Credo Mobile/Sprint MVNO - Telispire/Mitel Solutions/Sprint Wholesale/CBeyond", "Mobily", "SaskTel", "O2 CZ (Eurotel)/O2 Slovakia", "Hutchison India", "Reliance/Reliance CDMA", "Verizon Wireless/Verizon Wireless Wholesale - Deltacom/Verizon Wireless Wholesale - Telispire/Verizon Long Distance LLC/Payless", 
		"Vodafone Turkey", "Meditel", "Indosat/StarOne", "Alcatel Shanghai Bell", "3-Sweden/3-Italy/3-UK/3-Austria/3-Ireland", "Vodafone India", "Centennial Wireless/Centennial PR", "T-Mobile Austria - AT", "TMN Portugal", "Oi Brazil", "Telecom New Zealand", "3-Australia", "TSTT Trinidad and Tobago", "TATA DOCOMO/TATA CDMA", 
		"T-Mobile Croatia - HR/T-Mobile Macedonia - MC/T-Mobile Montenegro - MN", "BT Italy", "1&1", "MTS Mobility", "Virgin Mobile", "Orange Slovakia", "Taiwan Mobile(台湾大哥大)", "Orange Austria", "Vodafone Malta", "BASE", "Cellular South/C Spire", "China Mobile HK(中国移动香港)", "MTS Russia", "VimpelCom/MegaFon", 
		"Digitel", "SK Telecom", "Solo (Bell)", "Carphone Warehouse UK/Carphone Warehouse Spain/Carphone Warehouse France/Carphone Warehouse NL/Phones 4 U", "Cable & Wireless Group/20:20 Group/Poste Mobile Group/Brightpoint Group/Brightstar Group/LuxGSM Group/Transatel Group", "Iusacell Mexico/Iusacell GSM", "XL Indonesia", "Fido Solutions", 
		"Wind Italy", "Mobinil", "Chunghwa", "Metro PCS", "Nextel International - Mexico", "Nextel International - Peru", "Nextel International - Brazil", "Nextel International - Argentina", "Vodafone Qatar", "Visafone", "Etisalat Misr", 
		"Brightpoint Australia/Ingram Micro AU/Ingram Micro NZ/Vodafone New Zealand/Brightstar Australia/Telecom New Zealand/Vodafone Australia/Optus/Digicel Papua New Guinea", "Axis", "China Telecom(中国电信)", "Bell Mobility/Virgin Mobile/SaskTel", "Telus", "Q-Tel", "Phone House France", "Phone House Germany", "Phone House Netherlands", "3-Indonesia", 
		"Millicom (TIGO) - Honduras/Millicom (TIGO) - Guatemala/Millicom (TIGO) - El Salvador/Millicom (TIGO) - Colombia/Millicom (TIGO) - Paraguay/Millicom (TIGO) - Bolivia", "3-Italy", "Wind Canada", "SMART Indonesia", "Digicel", "Orange Dominicana", "DTAC", "IDEA", "Brightstar Hong Kong/China Mobile HK(中国移动香港)/Hutchison/HK Regional PRD/PCCW/SmarTone HK/SiS (Hong Kong)/CSL/SmarTone Macau", 
		"Open Mobile", "Digital China", "Virgin Mobile UK", "AMX Claro Colombia", "AMX Claro Ecuador/AMX Claro Costa Rica", "AMX Claro Argentina", "AMX Claro Brazil", "TracFone Wireless Inc", "3-Sweden", "3-Austria", "Boost Mobile", "SaskTel", "MTS Mobility", "Virgin Mobile", "Cricket", "BR Regional PRD", "Vodafone Ghana", "Koodo Mobile", 
		"Koodo Mobile", "Mobilicity", "AMX Claro Panama/AMX Claro El Salvador/AMX Claro Honduras/AMX Claro Guatemala/AMX Claro Dominican Republic/AMX Claro Nicaragua", "Brightstar Group/Intcomex Group/Nuevatel Group/ICE Group/Entel Group/Alegro Group/Antel Group/Telecom Group/Trilogy Group/Vox Group/ Group", "Virgin Mobile US", "Telefonica Argentina", 
		"Alegro Ecuador", "Videotron", "MTS India", "AMX Claro Jamaica", "AMX Claro Puerto Rico", "AMX Claro Dominican Republic/AMX Claro Puerto Rico", "Bakrie Telecom", "ATNI", "AMX Claro Chile", "Argentina Regional PRD/Brightstar Argentina", "China Unicom(中国联通)/Telling(天音科技)", "Virgin Mobile France", "Meteor Communication", "Yoigo Spain", 
		"VTR Wireless", "Free Mobile", "RIM OTASL", "Nextel International - Chile", "Nextel International - Mexico", "Nextel International - Peru", "Nextel International - Brazil", 
		"Axiom/Etisalat Misr/EMS/GeoCell/Etisalat Nigeria/DU/Emtel/GO Mobile/Batelco/MTC Touch/Mobinil/Mcel/Nawras/Oman Mobile/Orange Mauritius/Safaricom/Orange Jordan/STC Saudi Arabia/Zain Kuwait/Wataniya Kuwait/Airtel Nigeria/Airtel Malawi/Airtel Zambia", 
		"Brightstar AZLAN FR/Brightpoint France/Airtel Congo/Airtel Madagascar/20:20 France/Orascom/Orange Tunisia/Tigo - Senegal/Virgin Mobile France/Coriolis/Djezzy/Airtel Gabon/WANA/Togocell/Tunise Telecom/Telma/Moov Niger/Moov Gabon/Maroc/Moov CDI/Nedjma", "AzerCell/Viva Armenia/Tcell Indigo/Tcell Somoncom/UMC/Moldcell/EMS EU/Bakcell", 
		"TMN Portugal/Gibtelecom/Globul/Trigcom/Telenor Hungary (Pannon)/Telenor Denmark/Telenor Serbia/Telenor Norway/Siminn/Virgin Mobile UK/Brightpoint Netherlands/Cable & Wireless UK/Brightpoint UK/Brightstar AZLAN UK/LuxGSM/Jersey Telecom/Polkomtel/AMC", "Brightstar Group/Brightpoint Group/20:20 Group/Sunrise Group/Orange Group/Ingram Micro Group/Vodafone Group/O2 Group/E-Plus Group/ Group", 
		"Cosmote Greece/Wind Hellas/Vodafone Greece", "Partner/Pelephone/Cellcom Wireless", "Safaricom/Airtel Zambia/Airtel Uganda/Airtel Nigeria/Airtel Ghana/Airtel Malawi/Airtel Tanzania/Zantel/Airtel Kenya/EMS Africa/Globacom/Emtel/Etisalat Nigeria/MTC/Mcel/Cell C/Brightstar South Africa/Telkom SA/Virgin Mobile SA/Tigo - Tanzania/UTL", 
		"MPulse/Brightpoint Singapore/Docomo Pacific/DiGi/Grameenphone/TMIC Cambodia/Qool Labs Singapore/Brightstar Vietnam/Brightstar Singapore/Brightstar Malaysia/CamGSM/Etisalat Lanka/Viettel/Mobitel Srilanka/DST Communication Brunei/SMART Philippines/PTI", "True Move/AIS Thailand/SiS (Thailand)/I-Mobile PLUS/DTAC/Realmove", 
		"Aircel/Redington/BSNL/MTNL/Loop Mobile/Etisalat Lanka/Grameenphone/Ingram India/Vodafone India/Bharti (Airtel)", "Intcomex Colombia/Mobility/Nuevatel Bolivia/SETAR/Antel/Alegro Ecuador/Viaero/UTS/West Central Wireless/Cellular One/BTC Bahamas/Comcel Haiti/Trilogy/Telecom Personal/Telem/Telesur/i wireless/Vitelcom/Long Lines/Bermuda Digital Communications/Entel", 
		"Brightpoint Indonesia/Indosat/Telkomsel/XL Indonesia/3-Indonesia/Axis", "SunCellular/Brightpoint Philippines", "Telling(天音科技)", "Brightpoint Malaysia/Brightstar Malaysia/Umobile/TuneTalk/DiGi/Vodafone Celcom/Maxis", "Brightpoint Singapore/Brightstar Singapore/Vodafone M1/SingTel/Qool Labs Singapore/StarHub/DST Communication Brunei", 
		"MobiFone/Viettel/Brightstar Vietnam/Vinaphone/Smartcom", "VimpelCom/MegaFon/MTS Russia", "Chunghwa/Brightstar Taiwan/Taiwan Mobile(台湾大哥大)", "Teliasonera Finland/Telenor Denmark/Trigcom Norway/Telenor Norway/Telenor Sweden/Tele2 Sweden/CUBIO Finland/Brightpoint Denmark/TDC Denmark/3-Sweden", "AVEA/Vodafone Turkey/Turkcell", "Sympac", 
		"RIM WebSL Zero (EDT工程机)", "RIM WLAN", "非正常销售、非正常工作状态手机"};
	public static final int ListRowHeightOffset = 13;
	public static final int CORNER_RADIUS = 16;
	public static final int COLOR_BORDER = 0xBBBBBB;
	//SKU: 0x39f4cc3c74302ee2L

	private Vector _elements;
	private static final int NICKNAME = 0;
	private static final int USE_ENGLISH = 1;
	private static final int SHORTCUT_KEY = 2;

	//Persistent objects
	private static Vector _data;
	private static PersistentObject _store;

	private String _nickName = "you";
	private boolean _useEnglish = false;
	private int _shortcutKey = ShortcutKey.DEFAULT_KEY;

	public RedFlowerAppConfig()
	{
		_elements = new Vector(3);

		for (int i=0; i<_elements.capacity(); i++)
		{
			_elements.addElement(new Object()); //String(""));
		}
	}

	//private boolean getElementBoolean(int id)
	//{
	//	String value = _elements.elementAt(id).toString();

	//	return ((value != null) && value.equalsIgnoreCase("true"));
	//}

	private boolean getElementBoolean(int id)
	{
		boolean result = false;
		Object valueObject = _elements.elementAt(id); 

		if (valueObject instanceof Boolean)
		{
			result = ((Boolean) valueObject).booleanValue();
		}

		return result;
	}

	//private boolean getElementBoolean(int id) {return ((Boolean) _elements.elementAt(id)).booleanValue();}

	private int getElementInt(int id) {return Integer.parseInt(_elements.elementAt(id).toString());}

	private String getElementString(int id) {return (String) _elements.elementAt(id);}

	public void setElement(int id, Object value) {_elements.setElementAt(value, id);}

	public void setNickname(String value)
	{
		setElement(NICKNAME, new String(value));
		_nickName = value;
	}

	public String getNickname() {return _nickName;}

	public void setUseEnglish(boolean value)
	{
		setElement(USE_ENGLISH, new Boolean(value));
		_useEnglish = value;
	}

	public boolean isUseEnglish() {return _useEnglish;}

	public void setShortcutKey(int value)
	{
		setElement(SHORTCUT_KEY, new Integer(value));
		_shortcutKey = value;
	}

	public int getShortcutKey() {return _shortcutKey;}

	public void clean()
	{
		synchronized (_store)
		{
			_store.setContents(new Vector());
			_store.forceCommit();

			return;
		}
	}

	public void initialize()
	{
		synchronized(_store)
		{
			try {
				_data = (Vector) _store.getContents();

				if(!_data.isEmpty())
				{
					_elements = (Vector) _data.lastElement();

					_nickName = getElementString(NICKNAME);
					_useEnglish = getElementBoolean(USE_ENGLISH);
					_shortcutKey = getElementInt(SHORTCUT_KEY);
				} else{
					makeDefaultSettings();
				}
			} catch (Exception e) {
				_store.setContents(new Vector());
				_store.forceCommit();
				_data = new Vector();

				makeDefaultSettings();
			}
		}
	}

	public void save()
	{
		setNickname(_nickName);
		setUseEnglish(_useEnglish);
		setShortcutKey(_shortcutKey);

		_data.addElement(_elements);

		synchronized(_store)
		{
			_store.setContents(_data);
			_store.commit();
		}
	}

	private void makeDefaultSettings()
	{
		setNickname("you");
		setUseEnglish(false);
		setShortcutKey(ShortcutKey.DEFAULT_KEY);

		_data.addElement(_elements);

		synchronized(_store)
		{
			_store.setContents(_data);
			_store.commit();
		}
	}

	static
	{
		_store = PersistentStore.getPersistentObject(0xa67510429b7a2ae2L); //blackberry_redflower_written_by_Walkline
	}
}